package br.com.SLLM;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import br.com.control.ConsultaGeral;
import br.com.control.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Tela_preenchimento_laudos extends Activity implements
		OnClickListener {

	private static String ImagemLocation = "http://192.168.1.135:8080/SLLM-REST(WebService)/SLLM/getImagem;laudoid=";// Url do webservice
	private ImageView imagemExame;
	private Button Voltar;
	private Bitmap bitmap;
	private URL imageURL = null;

	// variaveis do metodo TrazerImagem
	ConsultaGeral consultaGeral;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tela_preenchimento_laudos);
		consultaGeral = (ConsultaGeral) getIntent().getSerializableExtra(
				"consultageral");
		// Chamada dos metodos:
		Atributos();
		TrazerImagem();
	}

	public void Atributos() {
		Voltar = (Button) findViewById(R.id.btnVoltarLista);
		Voltar.setOnClickListener(this);
		imagemExame = (ImageView) findViewById(R.id.imageview);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, TelaLista_laudo.class);
		startActivity(intent);
		finish();

	}

	public void TrazerImagem() {
		try {
			imageURL = new URL(ImagemLocation + consultaGeral.getLaudoid());//ip do webservice mais o parametro da lista
			HttpGet httpRequest = null;
			httpRequest = new HttpGet(imageURL.toURI());
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = null;

			try {
				response = (HttpResponse) httpclient.execute(httpRequest);
			} catch (ClientProtocolException e) {
				Log.d("Erro ao executar o htttpcliente",e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
			}

			HttpEntity entity = response.getEntity();
			BufferedHttpEntity bufHttpEntity = null;
			try {
				bufHttpEntity = new BufferedHttpEntity(entity);
			} catch (IOException e) {
				Log.d("Erro no bufHttpEntity", e.getMessage());
			}
			InputStream instream = null;
			try {
				instream = bufHttpEntity.getContent();
			} catch (IOException e) {
				Log.d("Erro na instream", e.getMessage());
			}
			bitmap = BitmapFactory.decodeStream(instream);
			imagemExame.setImageBitmap(bitmap);

		} catch (Exception e) {
			Log.d("Erro no Parametro!", e.getMessage());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.tela_preenchimento_laudos, menu);
		return true;
	}

}
